<template>
  <div>
    <form-head @onSubmit="onSubmit" :is_change="is_change"/>
    <el-form ref="form" :rules="rules" :model="form" label-width="100px" class="layout-all form-width">
      <el-form-item label="商品类名:" prop="title">
        <el-input v-model="form.title" placeholder="请输入商品类名" />
      </el-form-item>
      <!-- <el-form-item label="上级:" prop="pid">
        <el-select v-model="form.pid" placeholder="请选择上级类别" @change="cg_select($event)">
          <el-option
            v-for="item in parent"
            :key="item.id"
            clearable
            :label="item.title"
            :value="item.id"
            style="width: 100%;"
          />
        </el-select>
      </el-form-item> -->
      <el-form-item label="封面:">
        <upload-pic :thumb="form.thumb" @update:thumb="form.thumb=$event" width="100"/>
      </el-form-item>
      <el-form-item label="排序:" prop="sort">
        <el-input-number v-model="form.sort" label="排序" />
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name:'goods_scat_form',
  data() {
    return {
      id: 0,
      is_change: undefined,
      parent: [],
      form: {
        id: 0,
        pid: '',
        title: '',
        sort: 0,
        thumb: ''
      },
      rules: {
        title: [{
          required: true,
          message: '请输入商品类名',
          trigger: 'blur'
        }]
      }
    }
  },
  mounted() {
    this.id = (this.$route.params && this.$route.params.id)||0
    this.$setTagsViewTitle(this.id)
    this.init_into()
  },
  methods: {
    onSubmit(formName) {
      const form = this.form
      console.log(form)
      this.$refs[formName].validate(valid => {
        if (valid) {
          form.id = this.id
          this.get('/shop/ShopGoodsCate/save', form, 'POST').then(e => {this.$back()})
          
        } else {
          console.log('error submit!!')
          return false
        }
      })
      console.log('submit!')
    },
    bk() {
      this.$router.back()
    },
    init_into() {
      this.loading = true
      this.get('/shop/ShopGoodsCate/info?id=' + this.id, 'POST').then(e => {
        this.parent = e.parent
        setTimeout(()=>{this.is_change=false},100)
      })
    },
    cg_select(event) {
      this.form.sort = event
    },
  },
  watch: {
    form:{
      handler (val, oldVal) {
        if(this.is_change!=undefined)
          this.is_change=true
      },
      deep:true
    }
  }
}
</script>

<style>
.layout-all {
  margin: 10px;
}
</style>
